
package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import com.google.gson.Gson;
import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import com.joysuch.wwyt.core.enums.UserRoleTypes;
import com.joysuch.wwyt.core.service.BaseTypeGroupItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ImportBpContractorBusinessTypeConvertor implements FieldValueConvert<String, String> {

    @Autowired
    private BaseTypeGroupItemService baseTypeGroupItemService;

    @Override
    public String convert(Object bean, String value, DataDumpField dataField) {
        if(value == null) {
            return null;
        }
        String[] ss = value.split("\n");
        List<String> errors = new ArrayList<>(ss.length);
        List<Long> ids = new ArrayList<>(ss.length);
        for(String s : ss) {
            if(s.trim().length() > 0) {
                BaseTypeGroupItem item = baseTypeGroupItemService.findByGroupNameAndItemName("承包商业务类型", s);
                if(item == null) {
                    errors.add("承包商业务类型 【" + s +"】 不存在");
                } else {
                    ids.add(item.getId());
                }
            }
        }
        if(errors.size() > 0) {
            throw new IllegalArgumentException(StringUtils.join(errors));
        }
        return new Gson().toJson(ids);
    }

}
